package com.cqut.forum.dao.imp;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.cqut.forum.dao.MessagesDao;
import com.cqut.forum.entity.Messages;

/**
 * 留言数据访问接口实现类
 * @author yangxun
 */
@Component
public class MessagesDaoImp extends BaseDaoImp implements MessagesDao {
	
	@Resource
	private HibernateTemplate hibernateTemplate;
	
	@SuppressWarnings("unchecked")
	public List<Messages> getByState(String state, int size) {
		String hql = "select * from Messages where messagesState = ?";
		Query query = super.getQuery(hql);
		query.setString(0, state);
		query.setFirstResult(0);
		query.setMaxResults(size);
		
		List<Messages> list = query.list();
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Messages> getByTime(Date date) {
		String  hql = "select *　from Messages where and messagesState!=\"删除\" and messagesTime = ?";
		Query query = super.getQuery(hql);
		query.setTime(0, date);
		
		List<Messages> list = query.list();
		return list;
	}
	@SuppressWarnings("unchecked")
	public List<Messages> getByLeaveUser(int leaveUserId) {
		String hql = "select * from Messages where leaveUserId = ? and messagesState!=\"删除\" ";
		Query query = super.getQuery(hql);
		query.setInteger(0, leaveUserId);
		
		List<Messages> list = query.list();
		return list;
	}

	@SuppressWarnings("unchecked")
	public int getUnreadSize(int acceptUserId) {
		String hql = "select count(*) from Messages m where m.acceptUserId = ? and m.messagesState = ?";
		Query query = super.getQuery(hql);
		
		query.setInteger(0, acceptUserId);
		query.setString(1, "未读");
		List<Long> list = query.list();
		return (int)(long)list.get(0);
	}

	public List<Messages> getUnread(int acceptUserId) {
		//String hql = "select m from Messages m where m.acceptUserId = ?";
		
		return null;
	}
	/************get and set method*****************/
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getMessages(int userId, int index, int size) {
		/*String sql = "select un.news_contents contents,un.user_news_id messageId,u.user_id userId,u.user_name userName," +
					 "u.user_photo userPhoto,un.news_time newsTime from user_news un,user u " +
					 "where un.user_id=u.user_id and " +
					 "un.accept_user_id=? and un.module_id=? order by un.news_time desc";*/
		
/*		String sql1 = "select u.user_id,u.user_photo,u.user_name,un.user_news_id,un.news_contents,un.news_time " +
					 "from user u,user_news un where un.module_id=? and un.user_id=u.user_id and un.accept_user_id=? " +
					 "un.previous_news_id = un.user_news_id order by un.news_time desc";
*/		String sql1 = "select u.user_id,u.user_photo,u.user_name,m.messages_contents,m.messages_time,m.messages_id " +
  					 "from user u,messages m where m.leave_user_id=u.user_id and m.accept_user_id=? " +
					 "and m.above_messages_id<0 and m.messages_state!=\"删除\" order by m.last_reply_time desc";
		List<Object> parameters = new ArrayList<Object>();
		parameters.add(userId);
		
		Map<String, Integer> pages = new HashMap<String, Integer>();
		pages.put("index", index);
		pages.put("size", size);
		
		List<Map<String, Object>> result = (List<Map<String, Object>>)super.excuteSql(sql1, parameters, pages);
		return result;
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getMessageReply(List<Integer> articleIds){
		String sql = "select u.user_id,u.user_photo,u.user_name,m.messages_contents,m.messages_time,m.messages_id," +
					 "m.above_messages_id " +
					 "from user u,messages m " +
					 "where m.leave_user_id = u.user_id and m.above_messages_id in (";

		List<Object> parameters = new ArrayList<Object>();
		for(Integer i : articleIds){
			sql = sql + "?,";
			parameters.add(i);
		}
		sql = sql.substring(0,sql.length()-1)+") order by m.messages_time";
		//调用父类中的方法获取数据
		List<Map<String, Object>> result = (List<Map<String, Object>>) super.excuteSql(sql, parameters, null);
		
		return result;
	}
	
	@Override
	public boolean haveReadMyMessages(int userId) {
		String sql = "update messages m set m.messages_state = \"已读\" where m.accept_user_id=? ";
		Query query = super.getSqlQuery(sql);
		query.setInteger(0, userId);
		//query.setInteger(1, moduleId);
		query.executeUpdate();
		return true;
	}

}
